from django.shortcuts import render

from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework import serializers
from .models import Activity


class ActivitySerializer(serializers.ModelSerializer):
    '''活动列表页面序列化器'''

    class Meta:
        model = Activity
        fields = ('id', 'title', 'image', 'city', 'start_time')


class ActivitiesSerializer(serializers.ModelSerializer):
    '''活动列表页面序列化器'''

    class Meta:
        model = Activity
        # fields = ('id', 'title', 'image', 'city', 'start_time')
        exclude = ('is_delete',)


class ActivitiesViewSet(ReadOnlyModelViewSet):
    '''活动首页视图'''
    queryset = Activity.objects.filter(is_delete=False).order_by('-start_time')

    def get_serializer_class(self):
        if self.action == 'list':
            return ActivitySerializer
        else:
            return ActivitiesSerializer
